<?php

class Default_IndexController extends Zend_Controller_Action 
{    
	public function preDispatch(){
		$this->tplDirectory = "default/index/";
	}

    public function setLanguageAction()
    {
        $lang = trim($this->_request->getParam('languages', 'en'));

        $url = trim($this->_request->getParam('url', ''));
        if(empty($lang)){
            $this->_redirect('/');
           exit;
        }
        if(empty($url)){
            $url=$_SERVER['http_host'];
        }
        $langArr = array(
            'en',
            'cn'
        );
        if (!in_array($lang, $langArr)) {
            $lang = 'en';
        }
        setcookie("LANGUAGES", $lang, time() + 60 * 60 * 24 * 30, "/", $_SERVER['http_host']);
        $url=str_replace('===','/',$url);
        $this->_redirect('/'.$url);
    }

    public function indexAction()
    {
        //查找所有国家
        $tpl=isset($_COOKIE['LANGUAGES'])?$_COOKIE['LANGUAGES']:'en';
        $this->view->countrys = Default_Service_Country::getAll();;
        echo Cff::renderTpl($this->tplDirectory.$tpl."-index.tpl","layout");
    }
	
	public function howItWorksAction(){
		echo Cff::renderTpl($this->tplDirectory."how-it-works.tpl","layout");
	}
	
	public function contactUsAction(){
		echo Cff::renderTpl($this->tplDirectory."contact-us.tpl","layout");
	}
	
	public function userManagementAction(){
		$indexServiceObj = new Default_Service_Index();
		$loginInfo = $indexServiceObj->getLoginInfo();
		if(!isset($loginInfo->isLogin) || !$loginInfo->isLogin){//没有登录,直接输出并弹出登录框
			$this->view->showLoginBox = true;
			echo Cff::renderTpl($this->tplDirectory."user-management.tpl","user-layout");
			exit;
		}
		
		//已经登录
		echo Cff::renderTpl($this->tplDirectory."user-management.tpl","user-layout");
	}
	
	/**
	* layout 头部
	*/
	public function headerAction(){
		$indexServiceObj = new Default_Service_Index();
		$loginInfo = $indexServiceObj->getLoginInfo();
        $front = Zend_Controller_Front::getInstance();
        $controller=$front ->getRequest()->getControllerName();
        $module=$front ->getRequest()->getModuleName();
        $action=$front ->getRequest()->getActionName();

        $this->view->CURRENTURL=urlencode($module.'==='.$controller.'==='.$action);
		if(!isset($loginInfo->isLogin) || !$loginInfo->isLogin){//没有登录,直接输出并弹出登录框
			$this->view->notLogin = true;
		}
		echo $this->view->render($this->tplDirectory."header.tpl");
	}
	
	/**
	* user left 头部
	*/
	public function leftAction(){
		$serviceObj = new Merchant_Service_Customer();
		$customer = $serviceObj->getLoginInfo()->customer;
		if(!isset($customer["reg_step"]) || $customer["reg_step"] != 3){
			$this->view->continueRegister = true;
		}
		echo $this->view->render($this->tplDirectory."user-left.tpl");
	}
	
	/**
	* 检查登录
	*/
	public function checkLoginAction(){
		$loginInfo = Default_Service_Index::getInstance()->getLoginInfo();
		
		if(!isset($loginInfo->isLogin) || !$loginInfo->isLogin){
			$this->_redirect("/merchant/index/login");
			exit;
		}
	}
			
	/**
	* 注册用户留言
	*/
	public function registeredMessageAction(){
		$customer_id = trim($this->_request->customer_id);
		$message = trim($this->_request->message);		
		$serviceObj = Default_Service_Index::getInstance();
		$error = $serviceObj->checkRegisteredMessageData($customer_id,$message);
		if(!empty($error)){
			$data=array(
				'error'=>$error,
				'ask'=>0
			);
			die(Zend_Json::encode($data));
		}
		
		$customerInfo = Merchant_Service_Customer::get($customer_id);
		if(!$customerInfo){
			$error[]='User ID not exists!';
			$data=array('error'=>$error,'ask'=>0);	
			die(Zend_Json::encode($data));
		}
		
		$row = array(
			"first_name" => $customerInfo['customer_firstname'],
			"last_name" => $customerInfo['customer_lastname'],
			"email" => $customerInfo['customer_email'],
			"phone" => $customerInfo['customer_telephone'],
			"message" => $message,
			"create_date" => date("Y-m-d H:i:s")
		);
		$res = $serviceObj->addMessage($row,1);
		if($res){
			$data=array(
				'msg'=> 'Send Success!',
				'ask'=>1
			);
			die(Zend_Json::encode($data));
		}
	}
	
	/**
	* 非注册用户留言
	*/
	public function unregisteredMessageAction(){
		$first_name = trim($this->_request->first_name);
		$last_name = trim($this->_request->last_name);
		$email = trim($this->_request->email);
		$phone = trim($this->_request->phone);
		$message = trim($this->_request->message);
		$row = array(
			"first_name" => $first_name,
			"last_name" => $last_name,
			"email" => $email,
			"phone" => $phone,
			"message" => $message,
			"create_date" => date("Y-m-d H:i:s")
		);
		$serviceObj = Default_Service_Index::getInstance();
		$error = $serviceObj->checkUnregisteredMessageData($row);		
		if(!empty($error)){
			$data=array(
				'error'=>$error,
				'ask'=>0
			);
			
			die(Zend_Json::encode($data));
		}
		
		$res = $serviceObj->addMessage($row,0);
		if($res){
			$data=array(
				'msg'=> 'Send Success!',
				'ask'=>1
			);
			die(Zend_Json::encode($data));
		}

	}
	
	public function checkRegisterAction(){
		$serviceObj = new Merchant_Service_Customer();
		$customer = $serviceObj->getLoginInfo()->customer;
		if(!isset($customer["reg_step"]) || $customer["reg_step"] != 3){
			// $this->view->continueRegister = true;
			$this->_redirect("/merchant/index/step1");
			exit;
		}
	}
	
	public function registerAction(){
		echo Cff::renderTpl($this->tplDirectory."register.tpl","layout");
	}
	public function caculateAction(){
		$planService = new Merchant_Service_Plan();
		$skus = $this->_request->skus;
		$items = $this->_request->items;
		$pallets = $this->_request->pallets;
		$fee = $planService->caculatePlanTotal($skus,$pallets,$items,"USD");
		die(json_encode(round($fee,2)));
	}
	
	public function continueAction(){
		$skus = $this->_request->skus;
		$items = $this->_request->items;
		$pallets = $this->_request->pallets;
		$session = new Zend_Session_Namespace("choosePlan");
		$session->skus = $skus;
		$session->items = $items;
		$session->pallets = $pallets;
	}
	
	public function activateAction(){
	    $customer_id = $this->_request->pid;
		$customer_activate_code = $this->_request->pkey;
		$customerServiceObj = new Merchant_Service_Customer();
		$customerServiceObj->customerActivate($customer_id,$customer_activate_code);
		$this->_redirect("/merchant/index/step1");
		exit;
		
	}
	
	public function shipCalcAction(){
		$deliveryCountry = $this->_request->delivery_country;
		$packageWeight = $this->_request->package_weight;
		//$shipTypes = Default_Service_ShipType::getAllShipTypes();
		
		$arr=array('countryId'=>$deliveryCountry,'weight'=>$packageWeight);
		$obj = new Default_Service_ShipFeeCalc($arr);
		$fee = $obj->calcFee();
		$deliveryCountry = Default_Service_Country::getByValue($deliveryCountry);
        $newfee=array();
		$usDoller = Default_Service_Currency::getByCode('USD');
		
		foreach($fee as $k=>$v){
			$currency = Default_Service_Currency::getByCode($fee[$k]['currency_code']);
            $newfee[$k]['st_name'] = $fee[$k]['st_name'];
            $newfee[$k]['inter_fee'] = round($fee[$k]['inter_fee']*$currency['currency_rate']/$usDoller['currency_rate'],2);
            $newfee[$k]['currency']['currency_symbol_left']=$usDoller['currency_symbol_left'];
		}
        unset($fee);
		die(Zend_Json::encode(array('deliveryCountry'=>$deliveryCountry,'packageWeight'=>$packageWeight,'fee'=>$newfee)));
    }
	
	 
}